package com.lizhi.menu.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lizhi.menu.Dto.AddressDto;
import com.lizhi.menu.common.response.Result;
import com.lizhi.menu.domain.Address;
import com.lizhi.menu.service.IAddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 地址Controller
 *
 * @author lizhi
 * @date 2023-05-18
 */
@RestController
@RequestMapping("/system/address")
@RequiredArgsConstructor
@Api(tags = "地址管理")
public class AddressController{
    private final IAddressService addressService;

    /**
     * 查询地址列表
     */
    @GetMapping("/list")
    @ApiOperation("查询地址列表")
    public Result<Page<Address>> list(AddressDto dto) {
        return Result.ok(addressService.list(dto));
    }

    /**
     * 查询地址列表
     */
    @GetMapping("/my")
    @ApiOperation("查询我的地址列表")
    public Result<Page<Address>> me(AddressDto dto) {
        dto.setUserId(Integer.getInteger(StpUtil.getLoginId().toString()));
        return Result.ok(addressService.list(dto));
    }

    /**
     * 获取地址详细信息
     */
    @GetMapping(value = "/{id}")
    @ApiOperation("获取地址详细信息")
    public Result<Address> getInfo(@PathVariable("id") String id) {
        return Result.ok(addressService.getInfo(id));
    }

    /**
     * 新增地址
     */
    @PostMapping
    @ApiOperation("新增地址")
    public Result<Void> add(@RequestBody Address address) {
        address.setUserId(StpUtil.getLoginId().toString());
        addressService.add(address);
        return Result.ok();
    }

    /**
     * 修改地址
     */
    @PutMapping
    @ApiOperation("修改地址")
    public Result<Void> edit(@RequestBody Address address) {
        addressService.edit(address);
        return Result.ok();
    }

    /**
     * 删除地址
     */
    @DeleteMapping("/{id}")
    @ApiOperation("删除地址")
    public Result<Void> remove(@PathVariable String id) {
        addressService.remove(id);
        return Result.ok();
    }
}
